<#assign pojo=oneToManyPojo.getOne()>
<#assign many=oneToManyPojo.getMany()>
<#assign ones=oneToManyPojo.getOnes()>
<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${pojo.getRealPackageAndClassPath()}">
<#assign masterid = pojo.getDeclarationNameFirstLetterLower()+"Id">
import pathToRegexp from 'path-to-regexp'
import { get${declarationName}ById } from '../services/${declarationName}Service'

export default {

  namespace: '${declarationNameFirstLetterLower}Detail',

  state: {
    data: {},
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(() => {
        const match = pathToRegexp('/${declarationNameFirstLetterLower}/:id').exec(location.pathname)
        if (match) {
          dispatch({type: 'query', payload: {id: match[1]}})
        }
      })
    },
  },

  effects: {
    *query ({
      payload,
    }, { call, put }) {
      let { id } = payload
      const result = yield call(get${declarationName}ById, id)
      const {success, message, data} = result
      if (success) {
        yield put({
          type: 'querySuccess',
          payload: {
            data: data,
          },
        })
      } else {
        throw message
      }
    },
  },

  reducers: {
    querySuccess (state, {payload}) {
      const {data} = payload
      return {
        ...state,
        data,
      }
    },
  },
}
